/**
 * 
 */
package com.roc.newPage;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.roc.dao.DaoException;
import com.roc.page.BaseSearchCondition;

/**
 * @author Administrator
 *
 */
@Repository
public class PageDaoImpl implements IPageDao {
	private Logger logger = LogManager.getLogger(PageDaoImpl.class);
	//@Autowired
	@Resource(name = "sessionFactoryDg")
	SessionFactory sessionFactoryDg;

//	sessionFactoryDg sessionFactoryDg  =  (sessionFactoryDg) ApplicationContextUtils.getBean("sessionFactoryDg");

	/* (non-Javadoc)
	 * @see com.roc.dao.IPagenitionDao#findAll(com.roc.page.BaseSearchCondition, int, int)
	 */
	
	@SuppressWarnings("unchecked")
	public List<Object> findAll(BaseSearchCondition searchCondition,
			int startRow, int maxRow) throws DaoException {
		List<Object> searchRsList=new ArrayList<Object>();
		Session session=null;
		try{
			session=sessionFactoryDg.openSession();
			String hql=searchCondition.getFullSearchHQL(null);
			Query query=session.createQuery(hql);
			List<Object> params=new ArrayList<Object>();
			params=searchCondition.getParams();
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			query.setFirstResult(startRow);
			query.setMaxResults(maxRow);
			searchRsList=query.list();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query recordes error",e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return searchRsList;
	}
	
	
	@Override
	public List<Object> findAll(String hql,Object[] params, int startRow, int maxRow) throws DaoException {
		List<Object> searchRsList=new ArrayList<Object>();
		Session session=null;
		try{
			session=sessionFactoryDg.openSession();
			Query query=session.createQuery(hql);
			 
			if(params!=null&&params.length>0){
				for(int i=0;i<params.length;i++)
				  query.setParameter(i,params[i]);
			}
			query.setFirstResult(startRow);
			query.setMaxResults(maxRow);
			searchRsList=query.list();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query recordes error",e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return searchRsList;
	}

	/* (non-Javadoc)
	 * @see com.roc.dao.IPagenitionDao#count(com.roc.page.BaseSearchCondition)
	 */
	public int count(BaseSearchCondition searchCondition) throws DaoException {
		long rowCount=0;
		Session session=null;
		try{
			String hql=searchCondition.getFullRowCountHQL(null);
			session=sessionFactoryDg.openSession();
			Query query=session.createQuery(hql);
			List<Object> params=new ArrayList<Object>();
			params=searchCondition.getParams();
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			rowCount=(Long) query.uniqueResult();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query total recordes error",e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return (int)rowCount;
	}
	@SuppressWarnings("unchecked")
	public List<Object> findAllSql(BaseSearchCondition searchCondition,
			int startRow, int maxRow) throws DaoException {
		List<Object> searchRsList=new ArrayList<Object>();
		Session session=null;
		try{
			session=sessionFactoryDg.openSession();
			String sql=searchCondition.getFullSearchHQL(null);
			Query query=session.createSQLQuery(sql);
			List<Object> params=new ArrayList<Object>();
			params=searchCondition.getParams();
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			query.setFirstResult(startRow);
			query.setMaxResults(maxRow);
			searchRsList=query.list();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query recordes error",e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return searchRsList;
	}
	
	@Override
	public int countSql(BaseSearchCondition searchCondition)
			throws DaoException {
		int rowCount=0;
		Session session=null;
		String sql="";
		try{
			session=sessionFactoryDg.openSession();
			sql=searchCondition.getFullRowCountHQL(null);
			Query query=session.createSQLQuery(sql);
			List<Object> params=new ArrayList<Object>();
			params=searchCondition.getParams();
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++){
					query.setParameter(i,params.get(i));
				
				}
			}
			String str= (String)query.uniqueResult().toString();
			if(str !=null){
				rowCount = Integer.parseInt(str);
			}
		}catch (Exception e) {
			logger.error(e.getMessage()+sql,e);
			throw new DaoException("PageniionDaoImpl query total recordes error",e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return rowCount;
	}
	
	public List<Object> findAllByParams(String sql,List<Object> params,
			int startRow, int maxRow) throws DaoException {
		List<Object> searchRsList=new ArrayList<Object>();
		Session session=null;
		try{
			session=sessionFactoryDg.openSession();
			Query query=session.createSQLQuery(sql);
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++)
				  query.setParameter(i,params.get(i));
			}
			query.setFirstResult(startRow);
			query.setMaxResults(maxRow);
			searchRsList=query.list();
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query recordes error",e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return searchRsList;
	}
	
	@Override
	public int countByParams(String sql,List<Object> params)
			throws DaoException {
		int rowCount=0;
		Session session=null;
		try{
			session=sessionFactoryDg.openSession();
			Query query=session.createSQLQuery(sql);
			if(params!=null&&params.size()>0){
				for(int i=0;i<params.size();i++){
					query.setParameter(i,params.get(i));
				
				
				}
			}
			String str= (String)query.uniqueResult().toString();
			if(str !=null){
				rowCount = Integer.parseInt(str);
			}
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			throw new DaoException("PageniionDaoImpl query total recordes error",e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return rowCount;
	}
	
	
	public Object createQueryUniqueResult(String hql, Object[] param) throws DaoException {
		Session session=null;
		try {
			session=sessionFactoryDg.openSession();
			Query  query=session.createQuery(hql);
			
			if(param!=null&&param.length>0){
				for(int i=0;i<param.length;i++){
					query.setParameter(i, param[i]);
				}
			}
			return query.uniqueResult();
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询HQL失败[" + hql + "]", e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
	}
	
	public Object findObj(String sql,Object[] parmas) throws DaoException {
		Object  obj=null;
		Session session=null;
		try {
			session=sessionFactoryDg.openSession();
			SQLQuery query = session.createSQLQuery(sql);
			if(parmas!=null&&parmas.length>0){
				for(int i=0;i<parmas.length;i++){
					query.setParameter(i, parmas[i]);
				}
				
			}
			obj=query.uniqueResult();
		} catch (Exception e) {
			throw new DaoException("执行SQL语句失败[" + sql + "]", e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return obj;
	}
	
	
	public List<Object> queryAll(String hql,Object[] param) throws DaoException {
		Session session=null;
		try {
			session=sessionFactoryDg.openSession();
			Query query=session.createQuery(hql);
			if(param!=null&&param.length>0){
				for(int i=0;i<param.length;i++){
					query.setParameter(i, param[i]);
				}
			}
			return query.list();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new DaoException("查询HQL失败[" + hql + "]", e);
		} finally {
			if(session!=null){
				session.close();
			}
		}
	}
	
	public List<Object> querySqlAll(String sql, Object[] parmas) throws DaoException {
		List<Object> objList=new ArrayList<Object>();
		Session session=null;
		try {
			session=sessionFactoryDg.openSession();
			SQLQuery query = session.createSQLQuery(sql);
			if(parmas!=null&&parmas.length>0){
				for(int i=0;i<parmas.length;i++){
					query.setParameter(i, parmas[i]);
				}
				
			}
			objList=query.list();
		} catch (Exception e) {
			throw new DaoException("查询SQL语句失败[" + sql + "]", e);
		}finally {
			if(session!=null){
				session.close();
			}
		}
		return objList;
	}


	
}
